package com.risesoftware.battlebugs;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SetupActivity extends Activity
{
	private SetupSelectedListener setupListener;
	public static final String PREFS_NAME = "pref";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.setup);

		Spinner rows = (Spinner) this.findViewById(R.id.spinner1);
		Spinner columns = (Spinner) this.findViewById(R.id.spinner2);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.size_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

		rows.setAdapter(adapter);
		columns.setAdapter(adapter);

		Spinner timer = (Spinner) this.findViewById(R.id.spinner3);
		ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
				this, R.array.timer_seconds_array,
				android.R.layout.simple_spinner_item);
		adapter3.setDropDownViewResource(android.R.layout.simple_spinner_item);
		timer.setAdapter(adapter3);

		setupListener = new SetupSelectedListener();
		rows.setOnItemSelectedListener(this.setupListener);
		columns.setOnItemSelectedListener(this.setupListener);
		timer.setOnItemSelectedListener(this.setupListener);

	}

	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub
		super.onPause();

		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("rows", this.setupListener.numOfRows);
		editor.putInt("columns", this.setupListener.numOfColumns);
		editor.putInt("timer", this.setupListener.timerLimit);
	}

}

class SetupSelectedListener implements OnItemSelectedListener
{

	public int numOfRows;
	public int numOfColumns;
	public int timerLimit;

	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id)
	{
		switch (parent.getId())
		{
		case R.id.spinner1:
			this.numOfRows = Integer.parseInt(parent.getItemAtPosition(pos).toString());
			break;

		case R.id.spinner2:
			this.numOfColumns = Integer.parseInt(parent.getItemAtPosition(pos).toString());
			break;

		case R.id.spinner3:
			this.timerLimit = Integer.parseInt(parent.getItemAtPosition(pos).toString());
			break;
		}
		// Toast.makeText(parent.getContext(), "Selected " +
		// parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
	}

	public void onNothingSelected(AdapterView<?> arg0)
	{
	}

}
